package com.haoshuang.zhouzhoubang.ui.page.shop.address.area

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityServiceArea2Binding
import com.haoshuang.zhouzhoubang.model.bean.CityBean
import com.haoshuang.zhouzhoubang.ui.page.area.CityListAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *
 */
class ServiceAreaActivity3 : BaseVMActivity<ServiceAreaViewModel, ActivityServiceArea2Binding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "选择收货地址"
        }
    }

    var list = arrayListOf<CityBean>()
    var list2 = arrayListOf<CityBean>()
    var list3 = arrayListOf<CityBean>()
    lateinit var adpater: CityListAdapter
    lateinit var adpater2: CityListAdapter
    lateinit var adpater3: CityListAdapter
    override fun initData() {
        binding.tvBz.text=""
        adpater = CityListAdapter(list)
        adpater2 = CityListAdapter(list2)
        adpater3 = CityListAdapter(list3)
        binding.apply {
            rvList.layoutManager = LinearLayoutManager(this@ServiceAreaActivity3)
            rvList2.layoutManager = LinearLayoutManager(this@ServiceAreaActivity3)
            rvList3.layoutManager = LinearLayoutManager(this@ServiceAreaActivity3)
            rvList.adapter = adpater
            tvCz.clickWithTrigger {
                for (b in list){
                    b.isCheck=0
                }
                list2.clear()
                adpater2.notifyDataSetChanged()
                list3.clear()
                adpater3.notifyDataSetChanged()
                city1= ""
                city2= ""
                city3= ""
                adpater.notifyDataSetChanged()
            }
            tvCommit.clickWithTrigger {
                if (city3.isNullOrBlank()){
                    toast("请选择区")
                    return@clickWithTrigger
                }
                var intt=Intent()

                intt.putExtra("city1",city1)
                intt.putExtra("city2",city2)
                intt.putExtra("city3",city3)
                setResult(137,intt)
                finish()
            }
            adpater.setOnItemClickListener { adapter, view, position ->
               if (list[position].isCheck==0){
                   for (b in list){
                       b.isCheck=0
                   }
                   list[position].isCheck=1
                   vm.getData1("parent_id" to  list[position].id)
                   list3.clear()
                   adpater3.notifyDataSetChanged()
                   city2= ""
                   city3= ""
                   city_id= ""
                   city1= list[position].name
               }else{
                   list[position].isCheck=0
                   list2.clear()
                   adpater2.notifyDataSetChanged()
                   list3.clear()
                   adpater3.notifyDataSetChanged()
                   city1= ""
                   city2= ""
                   city3= ""
                   city_id= ""
               }
                adpater.notifyDataSetChanged()
            }
            rvList2.adapter = adpater2
            adpater2.setOnItemClickListener { adapter, view, position ->
                if (list2[position].isCheck==0){
                    for (b in list2){
                        b.isCheck=0
                    }
                    list2[position].isCheck=1
                    vm.getData2("parent_id" to  list2[position].id)
                    city2= list2[position].name
                    city3= ""
                    city_id= ""
                    list3.clear()
                    adpater3.notifyDataSetChanged()
                }else{
                    list2[position].isCheck=0
                    list3.clear()
                    adpater3.notifyDataSetChanged()
                    city2= ""
                    city3= ""
                    city_id= ""
                }
                adpater2.notifyDataSetChanged()
            }
            rvList3.adapter = adpater3
            adpater3.setOnItemClickListener { adapter, view, position ->
                if (list3[position].isCheck==0){
                    for (b in list3){
                        b.isCheck=0
                    }
                    list3[position].isCheck=1
                    city3= list3[position].name
                    city_id= list3[position].id.toString()
                }else{
                    list3[position].isCheck=0
                    city3= ""
                    city_id= ""
                }
                adpater3.notifyDataSetChanged()
            }
        }
        vm.getData()
    }
        var city1=""
        var city2=""
        var city3=""
        var city_id=""
    override fun startObserve() {
        vm.getData1.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code!="402"){
                    if (code!="402"){
                        toast(msg)
                    }
                }
            }
            onSuccess {
                dismissLoading()
                list2.clear()
                list2.addAll(it!!)
                adpater2.notifyDataSetChanged()
            }
        }
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code!="402"){
                    if (code!="402"){
                        toast(msg)
                    }
                }
            }
            onSuccess {
                dismissLoading()
                list3.clear()
                list3.addAll(it!!)
                adpater3.notifyDataSetChanged()
            }
        }
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code!="402"){
                    if (code!="402"){
                        toast(msg)
                    }
                }
            }
            onSuccess {
                dismissLoading()
                list.clear()
                list.addAll(it!!)
                adpater.notifyDataSetChanged()
            }
        }
    }

}